package ysq.xyg.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.dao.ProductDao;
import ysq.xyg.model.Product;
import ysq.xyg.service.ProductService;

import javax.annotation.Resource;
import java.util.List;

@Service("productService")
public class ProductServiceImpl extends BaseServiceImpl<Product> implements ProductService {

    @Resource
    private ProductDao productDao;
    @Autowired
    public void setProductDao(ProductDao productDao) {
        this.baseDao = productDao;
    }

    @Override
    public PageInfo<Product> pageInfo(Product product, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy("sort desc");
        List<Product> list = list(product);
        return new PageInfo<Product>(list);
    }
    @Override
    public List<Product> list(Product product) {
        // 通用Example查询
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(product.getStatus())) {
            criteria.andEqualTo("status", product.getStatus());
        }
        if (StringUtils.isNotEmpty(product.getTitle())) {
            criteria.andLike("title", "%" + product.getTitle() + "%");
        }
        if (StringUtils.isNotEmpty(product.getType())) {
            criteria.andEqualTo("type", product.getType());
        }
        List<Product> list = productDao.selectByExample(example);
        return list;
    }
}
